package com.tydic.workflow.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.tydic.common.core.domain.model.LoginUser;
import com.tydic.common.core.exception.api.ApiException;
import com.tydic.common.satoken.utils.LoginHelper;
import com.tydic.workflow.common.constant.ProcessVariableKey;
import com.tydic.workflow.service.IAuditService;
import com.tydic.workflow.service.IBpmService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class AuditServiceImpl implements IAuditService {

    protected abstract IBpmService getBpmService();

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submit(String processInstanceId, boolean pass) {
        submit(processInstanceId, null, pass);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submit(String processInstanceId, String comment, boolean pass) {
        submit(processInstanceId, null, comment, pass);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submit(String processInstanceId, Map<String, Object> variables, String comment, boolean pass) {
        LoginUser user = LoginHelper.getLoginUser();
        Task task = getBpmService().getTask(processInstanceId, user.getUserCode());
        if (task == null) {
            task = getBpmService().getTask(processInstanceId, user.getLoginCode());
        }
        if (task == null) {
            throw new ApiException("审批任务不存在，请联系管理员");
        }
        Map<String, Object> newVariables = new HashMap<>();
        if (variables != null) {
            newVariables.putAll(variables);
        }
        newVariables.put(ProcessVariableKey.PASS, pass);
        if (StrUtil.isBlank(task.getAssignee())) {
            getBpmService().claim(task.getId(), user.getUserCode());
        }
        getBpmService().complete(task.getId(), newVariables, comment);
    }


    @Override
    public boolean isFinished(String processInstanceId) {
        HistoricProcessInstance historicProcessInstance = getBpmService().getHistoryProcessInstance(processInstanceId);
        if (historicProcessInstance == null) {
            throw new ApiException(String.format("流程ID:%s，该流程不存在", processInstanceId));
        }
        return historicProcessInstance.getEndTime() != null;
    }

    @Override
    public Map<String, Boolean> isFinished(Set<String> processInstanceIds) {
        List<HistoricProcessInstance> historyProcessInstances = getBpmService().getHistoryProcessInstances(processInstanceIds);
        Map<String, Boolean> result = new HashMap<>();
        if (CollUtil.isNotEmpty(historyProcessInstances)) {
            for (HistoricProcessInstance item : historyProcessInstances) {
                result.put(item.getId(), item.getEndTime() != null);
            }
        }
        return result;
    }
}
